import Vue from 'vue'
import store from './store'
import App from './App'

import './plugin/modal'
import './plugin/ajax'
import './plugin/dayjs'

import './mixin'
import './filter'

// import Json from './Json' //测试用数据
// const json = type => {
// 	//模拟异步请求数据
// 	return new Promise(resolve => {
// 		setTimeout(() => {
// 			resolve(Json[type]);
// 		}, 500)
// 	})
// }

const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

//区分店铺时 购物车处理
const getStoreCart = (store_id, cartarray) => {
	let ret = [];
	for (var i = 0; i < cartarray.length; i++) {
		if (cartarray[i].store_id == store_id) {
			ret.push(cartarray[i]);
		}
	}
	return ret;
}

const setCartBadge = (num) => {
	if (num > 0) {
		uni.setTabBarBadge({
			index: 1,
			text: num + ''
		})
	} else {
		uni.removeTabBarBadge({ index: 1 })
	}
}

const prePage = () => {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}


const putExtra = obj => {
	return encodeURIComponent(JSON.stringify(obj));
}

const getExtra = obj => {
	return JSON.parse(decodeURIComponent(obj));
}

//组件加载
import nv from "@/components/pyh-nv/pyh-nv.vue";
Vue.component("nv", nv);

import uniIcons from "@/components/uni-icons/uni-icons.vue"
Vue.component("uniIcons", uniIcons);
//end 组件


Vue.config.productionTip = false
Vue.prototype.$api = {
	// json,
	msg,
	prePage,
	putExtra,
	getExtra,
	getStoreCart,
	setCartBadge
};

App.mpType = 'app'

const app = new Vue({
	store,
	...App
})
app.$mount()
